package com.aj.activity;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.aj.activity.adapter.HomeFragmentPagerAdapter;
import com.aj.activity.fragment.FamilyFragment;
import com.aj.activity.fragment.HomeFragment;
import com.aj.activity.fragment.SettingsFragment;
import com.aj.activity.fragment.TaskFragment;
import com.aj.utils.Contants;

@SuppressLint({ "Override", "NewApi" })
public class HomeActivity extends FragmentActivity {
	private ViewPager mPager;
	private RadioGroup mGroup;
	private RadioButton familyRadioButton;
	private RadioButton homeRadioButton;
	private RadioButton settingsRadioButton;
	private TextView title;
	private TextView skinpeeler_button;
	private Button rightbutton_1;
	/**
	 * 双击退出函数
	 */
	private static Boolean isExit = false;

	private ArrayList<Fragment> fragmentList;

	private String pageFlag = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		// 初始化界面组件
		initView();
		// 初始化ViewPager
		initViewPager();
	}

	private void initView() {
		mPager = (ViewPager) findViewById(R.id.viewPager);
		mGroup = (RadioGroup) findViewById(R.id.radiogroup);
		familyRadioButton = (RadioButton) findViewById(R.id.rb_family);
		homeRadioButton = (RadioButton) findViewById(R.id.rb_home);
		settingsRadioButton = (RadioButton) findViewById(R.id.rb_settings);
		skinpeeler_button = (TextView) findViewById(R.id.skinpeeler_button);
		title = (TextView) findViewById(R.id.title);
		rightbutton_1 = (Button) findViewById(R.id.rightbutton_1);

		setActionTitile(R.string.radiobutton_myfamily);
		ButtonEvent buttonEvent = new ButtonEvent();
		skinpeeler_button.setOnClickListener(buttonEvent);
		rightbutton_1.setOnClickListener(buttonEvent);
		pageFlag = getIntent().getStringExtra(Contants.PAGEFLAG_STRING);
		// RadioGroup选中状态改变监听
		mGroup.setOnCheckedChangeListener(new myCheckChangeListener());
	}

	class ButtonEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.skinpeeler_button:
				Toast.makeText(getApplicationContext(), "点击了换肤",
						Toast.LENGTH_LONG).show();
				break;
			case R.id.rightbutton_1:
				Intent intent = new Intent(Contants.MORE_FAMILY_ACTIVITY_ACTION);
				startActivity(intent);
				break;
			}
		}

	}

	private void setActionTitile(int textResID) {
		title.setText(textResID);
	}

	private void initViewPager() {
		FamilyFragment familyFragment = new FamilyFragment();
		HomeFragment homeFragment = new HomeFragment();
		TaskFragment taskFragment = new TaskFragment();
		SettingsFragment settingsFragment = new SettingsFragment();
		fragmentList = new ArrayList<Fragment>();
		fragmentList.add(familyFragment);
		fragmentList.add(homeFragment);
		fragmentList.add(taskFragment);
		fragmentList.add(settingsFragment);
		// ViewPager设置适配器
		mPager.setAdapter(new HomeFragmentPagerAdapter(
				getSupportFragmentManager(), fragmentList));
		// ViewPager显示第一个Fragment
		if (pageFlag != null
				&& pageFlag.equals(Contants.REGISTER_PAGEFLAG_STRING)) {
			mPager.setCurrentItem(2);
			settingsRadioButton.setChecked(true);
		} else {
			mPager.setCurrentItem(0);
		}
		// ViewPager页面切换监听
		mPager.setOnPageChangeListener(new myOnPageChangeListener());
	}

	/**
	 * RadioButton切换Fragment
	 */
	private class myCheckChangeListener implements
			RadioGroup.OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.rb_family:
				// ViewPager显示第一个Fragment且关闭页面切换动画效果
				mPager.setCurrentItem(0, false);
				break;
			case R.id.rb_home:
				mPager.setCurrentItem(1, false);
				break;
			case R.id.rb_task:
				mPager.setCurrentItem(2, false);
				break;
			case R.id.rb_settings:
				mPager.setCurrentItem(3, false);
				break;
			}
		}
	}

	/**
	 * ViewPager切换Fragment,RadioGroup做相应变化
	 */
	private class myOnPageChangeListener implements
			ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {

		}

		@Override
		public void onPageSelected(int position) {
			switch (position) {
			case 0:
				mGroup.check(R.id.rb_family);
				setActionTitile(R.string.radiobutton_myfamily);
				break;
			case 1:
				mGroup.check(R.id.rb_home);
				setActionTitile(R.string.radiobutton_myhome);
				break;
			case 2:
				mGroup.check(R.id.rb_task);
				setActionTitile(R.string.radiobutton_mytask);
				break;
			case 3:
				mGroup.check(R.id.rb_settings);
				setActionTitile(R.string.radiobutton_mysetting);
				break;
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {

		}
	}

	/**
	 * 菜单、返回键响应
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitBy2Click();
		}
		return false;
	}

	private void exitBy2Click() {
		Timer tExit = null;
		if (isExit == false) {
			isExit = true; // 准备退出
			Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			tExit = new Timer();
			tExit.schedule(new TimerTask() {
				@Override
				public void run() {
					isExit = false; // 取消退出
				}
			}, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

		} else {
			finish();
			System.exit(0);
		}
	}
}
